package com.tic.robot.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.tic.robot.R;
import com.tic.robot.ui.fragment.ConnectFragment;
import com.tic.robot.ui.fragment.RobotFragment;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    ConnectFragment mConnectFragment;

    RobotFragment mRobotFragment;

    boolean mShowConnectView = false;

    private static Context sContext;

    public static Context getContext() {
        return sContext.getApplicationContext();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sContext = this;

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        initView();
    }

    private void initView() {
        TextView titleTv = (TextView) findViewById(R.id.toolbar_title);
        titleTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showConnectView();
            }
        });

        mConnectFragment = ConnectFragment.newInstance();
        mRobotFragment = RobotFragment.newInstance();

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.framelayout, mRobotFragment);
        transaction.add(R.id.framelayout, mConnectFragment);
        transaction.commit();

        showRobotView();
    }

    private void showConnectView() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.hide(mRobotFragment).show(mConnectFragment);
        transaction.commit();
        mShowConnectView = true;
    }

    private void showRobotView() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.show(mRobotFragment).hide(mConnectFragment);
        transaction.commit();
        mShowConnectView = false;
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            if (mShowConnectView) {
                showRobotView();
            } else {
                super.onBackPressed();
            }
        }
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}
